﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Prime : Form
    {
        public Prime()
        {
            InitializeComponent();
        }

        public bool IfPrime(int a)
        {
            if(a < 2)
            {
                return false;
            }
            for(int i = 2;i < Math.Sqrt(a); i++)
            {
                if (a % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

        string SetTextBox(int min,int max)
        {
            string temp = "";
            int count = 0;
            for (int i = min; i <= max; i++)
            {
                if (IfPrime(i))//如果是素数
                {
                    temp += Convert.ToString(i);
                    temp += " ";
                    count++;
                    if (count >= 10)
                    {
                        temp += "\r\n";
                        count = 0;
                    }
                }
            }
            return temp;
        }
        private void OnButtonClicked(object sender, EventArgs e)
        {
            try
            {
                int min = int.Parse(textBox1.Text);
                int max = int.Parse(textBox2.Text);
                textBox3.Text = SetTextBox(min, max);
            }catch(Exception a)
            {
                MessageBox.Show("输入数据不合要求，请重新输入!");
            }
            

        }
    }
}
